package DBLayer;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import ModelLayer.Discount;

public class DBDiscount implements IFDBGeneric<Discount> {
	private Connection con;

	public DBDiscount() {
		con = DbConnection.getInstance().getDBcon();
	}

	public ArrayList<Discount> getAll() {
		return miscWhere("");
	}

	public Discount find(String type) {
		return singleWhere("type = '" + type);
	}

	public Discount find_byCustomerId(int Cid) {
		return singleWhere("Cid = '" + Cid);
	}
	
	public int delete(int sid) {return 0;}

	public int insert(Discount disc) throws Exception {

		int rc = -1;
		String query = "INSERT INTO Discount(type, minAmount, discount)  VALUES('"
				+ disc.getType()
				+ "',"
				+ disc.getMinAmount()
				+ ","
				+ disc.getDiscount() + " )";

		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (SQLException ex) {
			System.err.println("Could not insert Discount obj with type "
					+ disc.getType());
		}

		return (rc);
	}

	public int update(Discount disc) {

		int rc = -1;

		String query = "UPDATE Discount SET minAmount =" + disc.getMinAmount()
				+ ", discount =" + disc.getDiscount() + " WHERE type='"
				+ disc.getType() + "'";

		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			System.out.println(query);
			rc = stmt.executeUpdate(query);

			stmt.close();
		} catch (Exception ex) {
			System.out.println("Update exception in Discount db: " + ex);
		}
		return (rc);
	}

	public int delete(String type) {
		int rc = -1;

		String query = "DELETE FROM Discount WHERE type = '" + type + "'";

		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			rc = stmt.executeUpdate(query);
			stmt.close();
		} catch (Exception ex) {
			System.out.println("Delete exception in Discount db: " + ex);
		}
		return (rc);
	}

	private ArrayList<Discount> miscWhere(String wClause) {
		ResultSet results;
		ArrayList<Discount> discounts = new ArrayList<Discount>();

		String query = buildQuery(wClause);

		try {
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			while (results.next()) {
				Discount disc = buildDiscount(results);
				discounts.add(disc);
			}
		} catch (Exception e) {
			System.out.println("Query exception - select: " + e);
		}

		return discounts;
	}

	
	private Discount singleWhere(String wClause) {
		ResultSet results;
		Discount disc = new Discount();

		String query = buildQuery(wClause);
		try { 
			Statement stmt = con.createStatement();
			stmt.setQueryTimeout(5);
			results = stmt.executeQuery(query);

			if (results.next()) {
				disc = buildDiscount(results);
				
				stmt.close();

			} 
		}
		catch (Exception e) {
			System.out.println("Query exception: " + e);
		}
		
		return disc;
	}

	private String buildQuery(String wClause) {
		String query = "SELECT type,minAmount,discount FROM Discount";

		if (wClause.length() > 0)
			query = query + " WHERE " + wClause;

		return query;
	}

	private Discount buildDiscount(ResultSet results) {
		Discount disc = new Discount();
		try { 
			disc.setType(results.getString("Type"));
			disc.setMinAmount(results.getDouble("MinAmount"));
			disc.setDiscount(results.getDouble("Discount"));

		} catch (Exception e) {
			System.out.println("error in building the Discount object");
		}
		return disc;
	}
}
